var firstCategoryData = {};
var secondCategoryData = {};
var thirdCategoryData = {};
var fourthCategoryData = {};
var categoryParams1 = [];
var categoryParams2 = [];
var categoryParams3 = [];
var categoryParams4 = [];
let categoryDataAry = [null, firstCategoryData, secondCategoryData, thirdCategoryData, fourthCategoryData]

/*获取分类列表
*params
* --categoryLevel -- 级别
* --categoryId -- 品类id */
function getCategory(categoryId, categoryLevel) {
    return new Promise((resolve, reject) => {
        var params = {}
        var ajaxUrl = "";
        if(!categoryId || categoryId == ""){
            params.category_level = categoryLevel;
            ajaxUrl = root.goodsInter.goods_listCategory; //1级品类调用接口
        }else {
            params.category_id = categoryId;
            ajaxUrl = root.goodsInter.goods_subCategory; //2、3、4级品类调用接口
            delete params.category_level;
        }
        web.doAjax(ajaxUrl, 'post', params, function (data) {
            if(data.errorCode != 0){
                reject(data);
            }
            resolve(data.data);
        },2);
    });
}
function queryCategoryListFn(categoryId, categoryLevel, isSelectAll, el){
    getCategory(categoryId, categoryLevel).then((result) => {
        var checkInp = $(el).parent().find("input");
        if(categoryLevel >= 2 && el && !checkInp.prop("checked")) {
            $(el).parent().find("input").trigger("click");
        }
        /*if(categoryLevel == 4) {
            result = [{
                "category_level": 4,
                "category_name": "橡套电缆",
                "parent_cate_id": 246,
                "category_id": 23511,
                "category_code": "010301"
            }, {
                "category_level": 4,
                "category_name": "焊把线",
                "parent_cate_id": 246,
                "category_id": 23611,
                "category_code": "010302"
            }]
        }*/
        var lastCategoryLevel = Number(categoryLevel) - 1;
        var NextCategoryLevel = Number(categoryLevel) + 1;
        var categoryListHtml = "";
        if(isSelectAll){
            var checkedStr = "checked = 'checked'";
        }
        result.forEach((item) => {
            let fn1 = getCategoryParentId(item.parent_cate_id, item.category_id);
            let category_id = fn1(categoryLevel - 2);
            let _key = "";
            if(categoryLevel == 1){
                item.checkedVal = false;
                item.selectAll = true;
                _key = item.category_id;
            }else {
                _key = category_id || item.parent_cate_id + '-' + item.category_id;
                if(categoryDataAry[categoryLevel].hasOwnProperty(_key)){
                    checkedStr = categoryDataAry[categoryLevel][_key][0].checkedVal ?  "checked = 'checked'" : "";
                    item.checkedVal = categoryDataAry[categoryLevel][_key][0].checkedVal;
                    item.selectAll =  categoryDataAry[categoryLevel][_key][0].selectAll;
                }else {
                    item.checkedVal = true;
                    item.selectAll = true;
                };
            }
            categoryDataAry[categoryLevel][category_id || _key] = [item];
            categoryListHtml +="<li class='pd-5'>\n" +
                "                    <input class='category_checked' name='classification01' type='checkbox' value='" + item.category_id + "' category-level='" + item.category_level + "' " + checkedStr + " onchange='checkCategoryFn(this," + JSON.stringify(result) + ")' class='checkbox'>\n" +
                "                    <label class='pl-10' onclick='queryCategoryListFn(" + item.category_id + "," + NextCategoryLevel + ", true, this)'>" + item.category_code + " " + item.category_name + "</label>\n" +
                "               </li>";
        });
        $("#classSet" + categoryLevel).html(categoryListHtml);
        $("#classDiv" + categoryLevel).show();
        hideClassDiv(categoryLevel);
    });
}
/*隐藏子级展示框*/
function hideClassDiv(categoryLevel) {
    for(let i = Number(categoryLevel);i < 5;i++){
        if(i > Number(categoryLevel)){
            $("#classDiv" + i).hide();
        }else {
            $("#classDiv" + i).show();
        }
    }
}
/*拼装对象key值*/
function getCategoryParentId(parent_cate_id, category_id) {
    return function (categoryLevel) {
        if (categoryLevel <= 0) {
            return false
        }
        for (let key in categoryDataAry[categoryLevel + 1]) {
            if (key.split('-')[categoryLevel] == parent_cate_id) {
                return key + '-' + category_id
            }
        }
    }
}
queryCategoryListFn("", 1);
/*选中品类函数*/
function checkCategoryFn(el, categoryData) {
    var categoryId = $(el).val();
    var categoryLevel = $(el).attr("category-level");
    var checkedVal = $(el).prop("checked");
    if(checkedVal){
        queryCategoryListFn(categoryId,Number(categoryLevel)+1, true);
        for (let key in categoryDataAry[categoryLevel]) {
            if (key.split('-')[categoryLevel - 1] == categoryId) {
                categoryDataAry[categoryLevel][key][0].checkedVal = true;
                if(JSON.stringify(categoryDataAry[categoryLevel]).indexOf("true") != -1){//修改父级checked的状态
                    let lastIndex = key.lastIndexOf("-");
                    let parentKey = key.substring(0, lastIndex);
                    categoryLevel != 1 ? categoryDataAry[categoryLevel - 1][parentKey][0].checkedVal = true : "";
                    categoryLevel != 1 ? categoryDataAry[categoryLevel - 1][parentKey][0].selectAll = true : "";
                    $(".category_checked[value=" + key.split('-')[categoryLevel - 2] + "]").prop("checked", true);
                }
            }
        }
    }else {
        hideClassDiv(categoryLevel);
        for (let key in categoryDataAry[categoryLevel]) {
            if (key.split('-')[categoryLevel - 1] == categoryId) {
                categoryDataAry[categoryLevel][key][0].checkedVal = false;
                let lastIndex = key.lastIndexOf("-");
                let parentKey = key.substring(0, lastIndex);
                categoryLevel != 1 ? categoryDataAry[categoryLevel - 1][parentKey][0].selectAll = false : "";
                if(JSON.stringify(categoryDataAry[categoryLevel]).indexOf("true") == -1){//修改父级checked的状态
                    categoryLevel != 1 ? categoryDataAry[categoryLevel - 1][parentKey][0].checkedVal = false : "";
                    $(".category_checked[value=" + key.split('-')[categoryLevel - 2] + "]").prop("checked", false);
                }
            }
        }
        for (var i = Number(categoryLevel);i < 5;i++){
            let _childCategoryDataArr = categoryDataAry[i];
            for (let childKey in _childCategoryDataArr) {
                if (childKey.split('-')[categoryLevel - 1] == categoryId) {
                    _childCategoryDataArr[childKey][0].checkedVal = false;
                    $(".category_checked[value=" + _childCategoryDataArr[childKey][0].category_id + "]").prop("checked", false);
                }
            }
        }
    }
}
function makeMap() {
    if(Object.keys(firstCategoryData).length > 0){
        categoryParams1 = [];
        for (const key1 in firstCategoryData) {
            if(firstCategoryData[key1][0].checkedVal){
                categoryParams1.push(firstCategoryData[key1][0].category_id);
            }
        }
    }
    if(Object.keys(secondCategoryData).length > 0){
        categoryParams2 = [];
        for (const key2 in secondCategoryData) {
            let _item = secondCategoryData[key2][0];
            if(_item.checkedVal && !firstCategoryData[_item.parent_cate_id][0].selectAll){
                categoryParams2.push(_item.category_id);
            }
            if(!_item.checkedVal){
                var index = categoryParams1.indexOf(_item.parent_cate_id);
                categoryParams1.splice(index,1);
            }
        }
    }
    if(Object.keys(thirdCategoryData).length > 0){
        categoryParams3 = [];
        for (const key3 in thirdCategoryData) {
            let _item = thirdCategoryData[key3][0];
            let lastIndex = key3.lastIndexOf("-");
            let parentKey = key3.substring(0, lastIndex);
            if(_item.checkedVal && !secondCategoryData[parentKey][0].selectAll){
                categoryParams3.push(_item.category_id);
            }
            if(!_item.checkedVal) {
                var index = categoryParams2.indexOf(thirdCategoryData[key3][0].parent_cate_id);
                categoryParams2.splice(index,1);
            }
        }
    }
    if(Object.keys(fourthCategoryData).length > 0){
        categoryParams4 = [];
        for (const key4 in fourthCategoryData) {
            let _item = fourthCategoryData[key4][0];
            let lastIndex = key4.lastIndexOf("-");
            let parentKey = key4.substring(0, lastIndex);
            if(_item.checkedVal && !thirdCategoryData[parentKey][0].selectAll){
                categoryParams4.push(_item.category_id);
            }
            if(!_item.checkedVal) {
                var index = categoryParams3.indexOf(_item.parent_cate_id);
                categoryParams3.splice(index,1);
            }
        }
    }
    console.log(firstCategoryData)
    console.log(categoryParams1);
    console.log(categoryParams2);
    console.log(categoryParams3);
    console.log(categoryParams4);
}